<?php

namespace app\admin\controller\order;

use app\admin\model\UM as UM;
use app\admin\service\TransactionService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use think\APP;
use think\Request as Request;

/**
 * Transaction 控制器
 * Class Transaction
 * @package app\admin\controller\order;
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 * @see \app\admin\validate\order\Transaction
 */
class Transaction
{
    // 请求参数
    private Request $request;

    // service
    private TransactionService $service;

    public function __construct(Request $request, TransactionService $transactionService)
    {
        $this->request = $request;
        $this->service = $transactionService;
    }

    /**
     * 列表
     * @param UM|null $user
     * @return ApiResponse
     * @url /order/transaction/index
     */
    public function index(UM $user): ApiResponse
    {
        $request = $this->request;

        $type = $request->param('type', 'company');
        $companyId = $request->param('companyId');
        $startTime = $request->param('startTime');
        $endTime = $request->param('endTime');
        $page = $request->param('page', 1);
        $page_size = $request->param('page_size', 20);

        return ApiResponse::success($this->service->index($user, $type, $companyId, $startTime, $endTime, $page, $page_size));
    }

    /**
     * 新增|修改
     * @param UM $user
     * @return ApiResponse
     * @url /order/transaction/save
     */
    public function save(UM $user): ApiResponse
    {
        $request = $this->request;

        $this->service->add($user, $request->param());

        return ApiResponse::success(null);
    }

}
